//
//  TabBarView.m
//  PetAPP
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 Yuming. All rights reserved.
//

#import "TabBarView.h"
#import "TableBarTouchView.h"
#import <DKNightVersion.h>

@interface TabBarView ()

@property (nonatomic, retain) TableBarTouchView *viewOfHome;

@property (nonatomic, retain) TableBarTouchView *viewOfshop;

@property (nonatomic, retain) TableBarTouchView *viewOfserver;

@property (nonatomic, retain) TableBarTouchView *viewOfmy;

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, copy) void (^block)(NSInteger);

@end

@implementation TabBarView



- (instancetype)initWithFrame:(CGRect)frame {

    
    self = [super initWithFrame:frame];
    if (self) {
        CGRect newFrame = (CGRect){.size = frame.size};
        
        [self createSmallViewWithFrame:frame];
        [self createSubViewsWithFrame:newFrame];
    }
    return self;
}
#pragma mark - 创建灰线
- (void)createSmallViewWithFrame:(CGRect)frame {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 1)];
    [self addSubview:view];
    view.layer.borderWidth = 0.5;
    view.layer.borderColor = [UIColor grayColor].CGColor;
    [view release];
}
#pragma mark - 创建
- (void)createSubViewsWithFrame:(CGRect)frame {
    self.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.2278 green:0.2278 blue:0.2278 alpha:1.0]);
    
    
    self.index = 1;
    CGFloat width = frame.size.width;
    CGFloat height = frame.size.height;
    
    /** 创建首页按钮 */
    self.viewOfHome = [[TableBarTouchView alloc] initWithFrame:CGRectMake(20, 5, 30, height - 10) image:@"home"];
    [self addSubview:_viewOfHome];
    [_viewOfHome release];
    self.viewOfHome.tag = 1;
    
    [self.viewOfHome passTag:^(TableBarTouchView *view) {
        [self changeImageWith:view];
    }];
    
    self.viewOfHome.imageView.dk_imagePicker = DKImageWithNames(@"home2", @"home2Night");
    
    /** 创建商店按钮 */
    self.viewOfshop = [[TableBarTouchView alloc] initWithFrame:CGRectMake((width - 120) / 6 + 40 , 5, 30, height - 10) image:@"shop"];
    [self addSubview:self.viewOfshop];
    [_viewOfshop release];
    self.viewOfshop.tag = 2;
    
    [self.viewOfshop passTag:^(TableBarTouchView *view) {
        [self changeImageWith:view];
    }];
    
    /** 创建照相机按钮 */
    
    /** 创建服务按钮 */
    self.viewOfserver = [[TableBarTouchView alloc] initWithFrame:CGRectMake((width - 120) / 6 * 4 + 20 * 4, 5, 30, height - 10) image:@"server"];
    [self addSubview:self.viewOfserver];
    [_viewOfserver release];
    self.viewOfserver.tag = 3;
    
    [self.viewOfserver passTag:^(TableBarTouchView *view) {
        [self changeImageWith:view];
    }];
    
    /** 创建我的页面按钮 */
    self.viewOfmy = [[TableBarTouchView alloc] initWithFrame:CGRectMake((width - 120) / 6 * 5 + 20 * 5, 5, 30, height - 10) image:@"my"];
    [self addSubview:self.viewOfmy];
    [_viewOfmy release];
    self.viewOfmy.tag = 4;
    
    [self.viewOfmy passTag:^(TableBarTouchView *view) {
        [self changeImageWith:view];
    }];
    
}

- (void)changeImageWith:(TableBarTouchView *)view{
    if (view.tag != self.index) {
        TableBarTouchView *tempView = [self viewWithTag:self.index];
        
        tempView.imageView.dk_imagePicker = DKImageWithNames([NSString stringWithFormat:@"%@1",tempView.imageName], [NSString stringWithFormat:@"%@1Night",tempView.imageName]);
        
        self.index = view.tag;
        
        view.imageView.dk_imagePicker = DKImageWithNames([NSString stringWithFormat:@"%@2",view.imageName], [NSString stringWithFormat:@"%@2Night",view.imageName]);
        self.block(view.tag);
    }
}

- (void)changeControllerViewWithTag:(void (^)(NSInteger))block {

    self.block = block;

}


- (void)dealloc {
    [_block release];
    [_viewOfHome release];
    [super dealloc];
}
@end
